--
-- Author: shunguo.chen
-- Date: 2016-06-21 15:03:21
--
-- IMInputPanel

local IMVoiceRender = import(".renders.IMVoiceRender")

local ClassRef = app.mvc.createPanelClass(...)

-- ClassRef._rcdNode = nil
-- ClassRef._rcdCancelNode = nil

ClassRef._editText = nil
ClassRef._voiceRender = nil

local MAX_WORD_LEN = 120

function ClassRef.create(node)
	local sprite = ClassRef.extend(node)
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._voiceRender = IMVoiceRender.create(self._ui["Panel_1/MicButton"], "current")

	self._ui.SetButton:addClickEventListener(function(sender)
			executeFunc(self._onOperate, "openSetView")
		end)
	
	self._ui.channel:addEventListener(function(sender)
			executeFunc(self._onOperate, "switchBtnMore", sender:isSelected())
		end)
	self._ui.btn_more:setVisible(false)

	self._ui.EnterButton:addClickEventListener(function(sender)
			local str = self._editText:getText()
			if string.len(str) > 0 then
				-- self._editText:setText("")
				executeFunc(self._onOperate, "enterWords", str)
			end
		end)

	self._ui.MoreButton:addEventListener(function(sender, eventType)
			-- print("IMInputPanel MoreButton click")
			executeFunc(self._onOperate, "switchChatBox", sender:isSelected())
		end)

	self._textField = self._ui["Panel_1/Input/TextField"]
	self._textField:setVisible(false)
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:setBoxBtnSelected(b)
	self._ui.MoreButton:setSelected(b)
end

function ClassRef:setChannelBtnSelected(b)
	self._ui.channel:setSelected(b)
end

function ClassRef:setChannelText(name, color)
	self._ui["Panel_1/channel/name"]:setString(name)
	color = display.color4BWithString(color or "#FFFFFF")
	self._ui["Panel_1/channel/name"]:setTextColor(color)
end

function ClassRef:setInputAble(b)
	self._ui.Input:setVisible(b)
	-- self._ui.MicButton:setTouchEnabled(b)
	-- self._ui.EnterButton:setTouchEnabled(b)
	-- self._ui.MoreButton:setTouchEnabled(b)

	-- self._ui.MicButton:setBright(b)
	-- self._ui.EnterButton:setBright(b)
	-- self._ui.MoreButton:setBright(b)
	
	self._ui.MicButton:setVisible(b)
	self._ui.EnterButton:setVisible(b)
	self._ui.MoreButton:setVisible(b)
end

function ClassRef:setEditBoxVisible(b)
	-- print("setEditBoxVisible")
	self._editText:setVisible(b)
end

function ClassRef:initEditBox()
	if self._editText then
		self._editText:removeFromParent()
		self._editText = nil
	end

	-- print("IMInputPanel.initEditBox")

	local function onEditCallback(strEventName,sender)
		-- print("IMInputPanel editEventName = " .. strEventName)
        local edit = sender 
		if strEventName == "began" then
        elseif strEventName == "ended" then
        elseif strEventName == "return" then
            local str = self._editText:getText()
            -- print("return str = " .. str)
            if string.len(str) > 0 then
            	if string.ulen(str) > MAX_WORD_LEN then
            		str = string.usubstr(str, MAX_WORD_LEN)
            	end
            	-- self._editText:setText("")
	        	executeFunc(self._onOperate, "enterWords", str)
            end
        elseif strEventName == "changed" then
            if string.ulen(edit:getText()) > MAX_WORD_LEN then                
                edit:setText(string.usubstr(edit:getText(), MAX_WORD_LEN))
            end
            executeFunc(self._onOperate, "wordsChange", edit:getText())
        end
	end

	self._editText = display.replaceTextFieldWithEditBox(self._textField, nil, nil, true)
	self._editText:registerScriptEditBoxHandler(onEditCallback)
end

function ClassRef:appendWords(word)
	local str = self._editText:getText()
	local strLen = string.ulen(str)
	local wordLen = string.ulen(word)
	if (strLen + wordLen) <= MAX_WORD_LEN then
		self._editText:setText(str .. word)
	end
end

function ClassRef:clearWords()
	self._editText:setText("")
end

return ClassRef